// pages/automobile/edit/edit.js
const request = require('../../../utils/request'); // 引入封装好的请求工具
const user = require('../../../utils/user');
const options = [{
    text: '小型车',
    value: 1,
  },
  {
    text: '大型车',
    value: 2,
  },
  {
    text: '电动车',
    value: 3,
  },
  {
    text: '摩托车',
    value: 4,
  },
  {
    text: '三轮车',
    value: 5,
  },
  {
    text: '其他车',
    value: 6,
  },
];
const options2 = [{
    "text": "白色",
    "value": "白色"
  },
  {
    "text": "灰色",
    "value": "灰色"
  },
  {
    "text": "黄色",
    "value": "黄色"
  },
  {
    "text": "粉色",
    "value": "粉色"
  },
  {
    "text": "红色",
    "value": "红色"
  },
  {
    "text": "紫色",
    "value": "紫色"
  },
  {
    "text": "绿色",
    "value": "绿色"
  },
  {
    "text": "蓝色",
    "value": "蓝色"
  },
  {
    "text": "棕色",
    "value": "棕色"
  },
  {
    "text": "黑色",
    "value": "黑色"
  },
  {
    "text": "其他",
    "value": "其他"
  }
];

const options3 = [{
    "text": "小型汽车",
    "value": 1
  },
  {
    "text": "大型汽车",
    "value": 3
  },
  {
    "text": "新能源",
    "value": 2
  },
  {
    "text": "香港车辆",
    "value": 4
  },
  {
    "text": "澳门车辆",
    "value": 5
  },
  {
    "text": "外籍车辆",
    "value": 6
  },
  {
    "text": "摩托车",
    "value": 7
  },
];

const options4 = [{
    "text": "蓝色",
  },
  {
    "text": "白色",
  },
  {
    "text": "黄色",
  },
  {
    "text": "黑色",
  },
  {
    "text": "农用绿",
  },
  {
    "text": "黄绿",
  },
  {
    "text": "绿色",
  },
  {
    "text": "其他",
  }
];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Vinfo: '',
    type: {
      1: "小型车",
      2: "大型车",
      3: "电动车",
      4: "摩托车",
      5: "三轮车",
      6: "其他车",
    },
    numType: {
      1: "小型汽车",
      2: "新能源",
      3: "大型汽车",
      4: "香港汽车",
      5: "澳门车辆",
      6: "外籍车辆",
      7: "摩托车",
    },
    brand: '',
    plate: '',
    show: false,
    show2: false,
    show3: false,
    show4: false,
    options,
    options2,
    options3,
    options4,
    fieldValue: '',
    cascaderValue: '',
    fieldValue2: '',
    cascaderValue2: '',
    fieldValue3: '',
    cascaderValue3: '',
    fieldValue4: '',
    cascaderValue4: '',
    fileList: [

    ],
    fileList2: [],
    addV: [],
    loading: false,
    myuser: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    user.getUserProfile().then(res => {
      this.setData({
        myuser: res.data
      })
    });
    // 获取URL参数
    const id = options.id // 
    console.log(id);
    const url = `/jy/vehicle_manage/${id}`;

    request.get(url, {
        params: {
          id: id
        }
      })
      .then(response => {
        console.log(response);
        console.log(response.data);
        // const item = response.data; // 单个对象
        this.setData({
          Vinfo: response.data
        });
      })
      .catch(error => console.error('Request Error:', error));
  },

  onClick() {
    this.setData({
      show: true,
    });
  },
  onClick2() {
    this.setData({
      show2: true,
    });
  },
  onClick3() {
    this.setData({
      show3: true,
    });
  },
  onClick4() {
    this.setData({
      show4: true,
    });
  },

  onClose() {
    this.setData({
      show: false,
    });
  },
  onClose2() {
    this.setData({
      show2: false,
    });
  },
  onClose3() {
    this.setData({
      show3: false,
    });
  },
  onClose4() {
    this.setData({
      show4: false,
    });
  },
  saveC(e) {
    const id = e.currentTarget.dataset.id;
    const updateUrl = `/jy/vehicle_manage`; // 修改接口 URL
    const dataToUpdate = this.data.Vinfo; // 获取当前 Vinfo 数据
    console.log("Vinfo======");
    console.log(this.data.Vinfo);
    console.log("dataToUpdate:  ==========");
    console.log(dataToUpdate);
    // 确保必要的字段存在，添加其他修改逻辑（如从表单获取新值）
    const updateData = {
      ...dataToUpdate,
      // 可根据需要动态更新字段，例如从输入框获取
      // plateNumber: this.data.newPlateNumber || dataToUpdate.plateNumber,
      // userId: this.data.myuser.userId // 确保用户 ID 更新
    };

    request.put(updateUrl, this.data.Vinfo) // 假设使用 PUT 方法
      .then(response => {
        console.log('修改成功:', response);
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        });
        // // 可选：刷新页面数据
        // this.onLoad({
        //   id
        // }); // 重新加载数据
      })
      .catch(error => {
        console.error('修改失败:', error);
        wx.showToast({
          title: '修改失败',
          icon: 'none',
          duration: 2000
        });
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onFinish(e) {
    const {
      selectedOptions,
      value
    } = e.detail;
    const fieldValue = selectedOptions
      .map((option) => option.text || option.name)
      .join('/');

      this.data.Vinfo.type = fieldValue
    this.setData({
      fieldValue,
      cascaderValue: value,
    })
  },

  onFinish2(e) {
    const {
      selectedOptions,
      value
    } = e.detail;
    const fieldValue2 = selectedOptions
      .map((option) => option.text || option.name)
      .join('/');
      
      this.data.Vinfo.color = fieldValue2
    this.setData({
      
      cascaderValue2: value
    })
    console.log("fieldValue2r==========");
    console.log(this.data.Vinfo.color);


  },
  onFinish3(e) {
    const {
      selectedOptions,
      value
    } = e.detail;
    const fieldValue3 = selectedOptions
      .map((option) => option.text || option.name)
      .join('/');
      this.data.Vinfo.numberType = fieldValue3
    this.setData({
      fieldValue3,
      cascaderValue3: value,
    })
  },
  onFinish4(e) {
    const {
      selectedOptions,
      value
    } = e.detail;
    const fieldValue4 = selectedOptions
      .map((option) => option.text || option.name)
      .join('/');
      this.data.Vinfo.plateColor = fieldValue4
    this.setData({
      fieldValue4,
      cascaderValue4: value,
    })
  },
  onChange(e){
    console.log(e.detail);
    const { value, callback } = e.detail;
    this.data.Vinfo.plate = value;
    this.setData({
      plate:value
    })
  },

  onChangebrand(e){
    console.log(e.detail);
    const { value, callback } = e.detail;
    this.data.Vinfo.brand = value;
    this.setData({
      brand:value
    })
  },
})